package com.example.stars_project_android.index;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;

import com.example.stars_project_android.R;
import com.example.stars_project_android.adapter.GridViewAdapter;
import com.example.stars_project_android.entity.SpaceTime;
import com.example.stars_project_android.util.Constant;

import java.util.ArrayList;
import java.util.List;

/**
 * 展示搜索结果的页面
 */
public class SearchReault extends AppCompatActivity {

    private EditText edit;   //搜索编辑框
    private Button btn;  //搜索按钮
    private GridView gridView;
    private ImageView delete;   //删除搜索框的内容
    private List<SpaceTime> list = new ArrayList<>();  //设置数据源
    private String search;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_reault);

        //接收跳转过来的搜索框的信息
        Intent intent = new Intent();
        search = intent.getStringExtra("search");

        //设置控件对象
        setView();

        /**
         * EditText添加监听
         */
        edit.addTextChangedListener(new TextWatcher() {
            //文本改变之前执行
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            //文本改变的时候执行
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //如果长度为0
                if (s.length() == 0) {
                    //隐藏“删除”图片
                    delete.setVisibility(View.GONE);
                } else {//长度不为0
                    //显示“删除图片”
                    delete.setVisibility(View.VISIBLE);
                }
            }
            //文本改变之后执行
            public void afterTextChanged(Editable s) { }
        });

        //设置搜索框不可编辑只可点击
        setCanNotEditAndClick();
        //设置数据源
        list = Constant.searchList;
//        list = setData();
        //设置适配器
        GridViewAdapter gridViewAdapter = new GridViewAdapter(
                this,
                R.layout.myroom_item,
                list
        );
        //绑定适配器
        gridView.setAdapter(gridViewAdapter);
    }

    private List<SpaceTime> setData() {
        List<SpaceTime> collections = new ArrayList<>();
        SpaceTime data1 = new SpaceTime();
        data1.setRoomName("万圣节一起挂机啊");
        collections.add(data1);
        SpaceTime data2 = new SpaceTime();
        data2.setRoomName("聊天时空");
        collections.add(data2);
        SpaceTime data3 = new SpaceTime();
        data3.setRoomName("聊天时空");
        collections.add(data3);
        SpaceTime data4 = new SpaceTime();
        data4.setRoomName("聊天时空");
        collections.add(data4);
        SpaceTime data5 = new SpaceTime();
        data5.setRoomName("聊天时空");
        collections.add(data5);
        return collections;
    }

    /**
     * 设置控件对象
     */
    private void setView() {
        edit = findViewById(R.id.edit);
        edit.setText(search);
        btn = findViewById(R.id.btn_search);
        gridView = findViewById(R.id.relative_layout);
        delete = findViewById(R.id.delete);
    }

    /**
     * 设置搜索框不可编辑只可点击
     */
    private void setCanNotEditAndClick() {
        edit.setFocusable(false);
        edit.setFocusableInTouchMode(false);
        edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳转到搜索页面
                Intent intent = new Intent(SearchReault.this, Search.class);
                startActivity(intent);
            }
        });
    }

}